package com.lucenly.spell.fragment

import android.content.Context
import android.graphics.Color
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.google.android.material.appbar.AppBarLayout
import com.lucenly.spell.R
import com.lucenly.spell.activity.MySpellActivity
import com.lucenly.spell.activity.MyStarSpellActivity
import com.lucenly.spell.activity.PeopleGuideActivity
import com.lucenly.spell.activity.ZeroSpellActivity
import com.lucenly.spell.adapter.ImageAdapter
import com.lucenly.spell.base.BaseFragment
import com.lucenly.spell.databinding.FragmentHomeBinding
import com.lucenly.spell.utils.DisplayUtil
import com.moregold.mvvmlib.ext.ext_doubleClick
import com.moregold.mvvmlib.ext.startActivity
import com.next.easynavigation.adapter.ViewPagerAdapter
import com.youth.banner.indicator.CircleIndicator
import com.youth.banner.indicator.RectangleIndicator
import com.youth.banner.indicator.RoundLinesIndicator
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.UIUtil
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ClipPagerTitleView

class HomeFragment : BaseFragment<FragmentHomeBinding>(R.layout.fragment_home) {
    val mTitleDataList = arrayListOf("3人团","5人团","10人团","15人团")
    private val fragmentList = arrayListOf<Fragment>()
    override fun getViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentHomeBinding {
        return FragmentHomeBinding.inflate(inflater)
    }

    override fun initData() {
        var list = arrayListOf<Int>()
        for (i in 1..5){
            list.add(i)
        }
        binding.banner.adapter = ImageAdapter(list)
        val indicator = RectangleIndicator(context)
        binding.banner.indicator = indicator
        binding.banner.setIndicatorHeight(DisplayUtil.dip2px(context!!,4f))
        binding.banner.setIndicatorNormalWidth(DisplayUtil.dip2px(context!!,12f))
        binding.banner.setIndicatorSelectedWidth(DisplayUtil.dip2px(context!!,12f))
        binding.banner.setIndicatorNormalColor(Color.parseColor("#55ffffff"))
        binding.banner.setIndicatorSelectedColor(Color.parseColor("#ffffff"))
        binding.banner.setBannerGalleryEffect(DisplayUtil.dip2px(context!!,12f),DisplayUtil.dip2px(context!!,12f),DisplayUtil.dip2px(context!!,5f),1f)

        val commonNavigator = CommonNavigator(context)
        commonNavigator.isAdjustMode = true
//        commonNavigator.setBackgroundResource(R.drawable.bg_shape_ffffff_10)
        commonNavigator.adapter = object : CommonNavigatorAdapter() {
            override fun getCount(): Int {
                return mTitleDataList.size
            }

            override fun getTitleView(context: Context?, index: Int): IPagerTitleView? {
                val clipPagerTitleView  = ClipPagerTitleView(context)
                clipPagerTitleView.clipColor = Color.WHITE
                clipPagerTitleView.textColor = Color.BLACK
                clipPagerTitleView.text = mTitleDataList[index]
                clipPagerTitleView.setOnClickListener {
                    binding.viewpager.currentItem = index
                }
                return clipPagerTitleView
            }

            override fun getIndicator(context: Context?): IPagerIndicator? {
                val indicator = LinePagerIndicator(context)
                val navigatorHeight = UIUtil.dip2px(context, 28.0).toFloat()
                indicator.lineHeight = navigatorHeight
                indicator.lineWidth = UIUtil.dip2px(context, 78.0).toFloat()
                indicator.roundRadius = UIUtil.dip2px(context, 14.0).toFloat()
                indicator.setColors(Color.parseColor("#ff0742"))
                indicator.mode = LinePagerIndicator.MODE_EXACTLY
                return indicator
            }
        }
        binding.tab.navigator = commonNavigator
        for (i in 1..mTitleDataList.size){
            var fragment : HomeItemFragment? = null
            if (i % 2==0){
                fragment = HomeItemFragment(Color.parseColor("#55ff66"))
            }else{
                fragment = HomeItemFragment(Color.parseColor("#846248"))
            }
            fragmentList.add(fragment)
        }
        binding.viewpager.adapter = ViewPagerAdapter(childFragmentManager, fragmentList)
        ViewPagerHelper.bind(binding.tab,binding.viewpager)
    }

    override fun initEvent() {
        binding.appBar.addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener { _, verticalOffset ->
            Log.e("=====",""+verticalOffset)
            context?.let {
                if (verticalOffset>-DisplayUtil.dip2px(it,150f)){
                    binding.llTitle.setBackgroundColor(Color.parseColor("#00000000"))
                    binding.appBar.setBackgroundColor(Color.parseColor("#00000000"))
                    binding.ivTitle.setImageResource(R.mipmap.sybtt)
                }else{
                    binding.llTitle.setBackgroundColor(Color.parseColor("#ffffff"))
                    binding.appBar.setBackgroundColor(Color.parseColor("#ffffff"))
                    binding.ivTitle.setImageResource(R.mipmap.sybtttt)
                }
            }
        })
        binding.llMySpell.ext_doubleClick {
            startActivity<MySpellActivity>()
        }
        binding.myStartSpell.ext_doubleClick {
            startActivity<MyStarSpellActivity>()
        }
        binding.llZero.ext_doubleClick {
            startActivity<ZeroSpellActivity>()
        }
        binding.llNewPeopel.ext_doubleClick {
            startActivity<PeopleGuideActivity>()
        }
    }

    override fun startObserve() {
    }
}